#
# Copyright 2008 Qumranet, Inc. All rights reserved.
# Use is subject to license terms.
#
#
FILES=define.py utils.py constants.py vm.py vdsm clientIF.py \
      guestIF.py dsaversion.py logCollector.sh \
      addNetwork delNetwork editNetwork \
      SecureXMLRPCServer.py ksm.py netinfo.py \
      get-conf-item set-conf-item \
      kaxmlrpclib.py config.py QemuMonitor.py img_verifier \
      write-net-config vdsm-restore-net-config vdsm-store-net-config

STORAGEFILES=storageConstants.py dispatcher.py storage_exception.py uuid.py \
             __init__.py \
             sd.py sp.py blockSD.py fileSD.py hsm.py safelease.py spm.py \
             image.py volume.py blockVolume.py fileVolume.py taskManager.py\
             resource.py storage_connection.py nfs.py misc.py\
             pv.py vg.py lvm.py lvmc.py iscsi.py multipath.py threadPool.py task.py \
             storage_mailbox.py cache.py sdc.py sdf.py metadata.py rwlock.py \
             syncspace.py threadLocal.py remoteFileHandler.py

PROTECTFILES=spmprotect.sh spmstop.sh safelease

VDSMDIR=/usr/share/vdsm
VDSMLOGDIR=/var/log/vdsm
VDSMRUNDIR=/var/vdsm
POOLSDATADIR=$(VDSMRUNDIR)/pools
BACKUPDIR=$(VDSMLOGDIR)/backup
BINDIR=$(VDSMDIR)
LIBEXECDIR=/usr/libexec/vdsm
CONFDIR=$(VDSMDIR)
TRUSTSTORE=$(VDSMRUNDIR)/ts

CFLAGS=-Wall -O

all: rpm

setdistro:
	if [ -e "/etc/fedora-release" ]; then cp constants.py.f11 constants.py; fi

# TODO fix this nonsense with autoconf
.PHONY: fixpaths
sudoers.vdsm: fixpaths sudoers.vdsm.in
	python -c "import constants,re,sys;s = sys.stdin.read();print re.sub('@[^@\n]*@', lambda m: getattr(constants, 'EXT_' + m.group()[1:-1], m.group()), s)," < sudoers.vdsm.in > $@

fixpaths:
	sed -i "s:@BINDIR@:$(BINDIR):;s:@CONFDIR@:$(CONFDIR):" img_verifier
	sed -i "s:@VDSMLOGDIR@:$(VDSMLOGDIR):" logger.conf
	sed -i "s:@VDSMRUNDIR@:$(VDSMRUNDIR):" vdsmd
	sed -i "s:@VDSMDIR@:$(VDSMDIR):;s:@CONFDIR@:$(CONFDIR):;" vdsmd
	sed -i "s:@VDSMDIR@:$(VDSMDIR):" constants.py{,.f11} vdsm-sosplugin.py
	sed -i "s:@VDSMRUNDIR@:$(VDSMRUNDIR):" constants.py{,.f11} storage/lvm.py vdsm-sosplugin.py
	sed -i "s:@POOLSDIR@:$(POOLSDATADIR):" config.py
	sed -i "s:@BACKUPDIR@:$(BACKUPDIR):" config.py
	sed -i "s:@LIBEXECDIR@:$(LIBEXECDIR):" config.py

install: tunctl storage/protect/safelease setdistro sudoers.vdsm
	mkdir -p $(PREFIX)$(VDSMDIR)
	mkdir -p $(PREFIX)$(VDSMDIR)/storage
	mkdir -p $(PREFIX)$(TRUSTSTORE)/{certs,keys}
	mkdir -p $(PREFIX)$(VDSMLOGDIR)
	mkdir -p $(PREFIX)$(VDSMRUNDIR)
	mkdir -p $(PREFIX)$(POOLSDATADIR)
	mkdir -p $(PREFIX)$(BACKUPDIR)
	mkdir -p $(PREFIX)$(BINDIR)
	mkdir -p $(PREFIX)$(LIBEXECDIR)
	mkdir -p $(PREFIX)$(CONFDIR)
	mkdir -p $(PREFIX)$(POOLDATADIR)
	mkdir -p $(PREFIX)$(BACKUPDATADIR)
	mkdir -p $(PREFIX)/etc/init.d/
	mkdir -p $(PREFIX)/etc/udev/rules.d/
	cp $(FILES) $(PREFIX)$(VDSMDIR)
	(cd storage; cp $(STORAGEFILES) $(PREFIX)$(VDSMDIR)/storage)
	cp tunctl $(PREFIX)$(BINDIR)
	cp vdsm.conf.sample logger.conf $(PREFIX)$(CONFDIR)
	cp vdsmd $(PREFIX)/etc/init.d/vdsmd
	cp 99-vdsm.rules $(PREFIX)/etc/udev/rules.d/99-vdsm.rules
	(cd storage/protect; cp $(PROTECTFILES) $(PREFIX)$(LIBEXECDIR))
	mkdir -p $(PREFIX)/etc/sudoers.d/
	cp sudoers.vdsm $(PREFIX)/etc/sudoers.d/50_vdsm

clean:
	$(RM) *~ *.pyc tunctl storage/protect/safelease
